package phone.example.com.a36kr.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

import phone.example.com.a36kr.R;
import phone.example.com.a36kr.activity.findactivity.FindRecentEventActivity;
import phone.example.com.a36kr.activity.findactivity.FindRecentGraduateActivity;
import phone.example.com.a36kr.adapter.NewsViewPagerAdapter;
import phone.example.com.a36kr.bean.NewsCarouselBean;
import phone.example.com.a36kr.nettool.NetTool;
import phone.example.com.a36kr.tools.BaseFragment;
import phone.example.com.a36kr.tools.MyApp;

/**
 * Created by dllo on 16/8/15.
 */
public class FindFragment extends BaseFragment {


    private NewsViewPagerAdapter viewPagerAdapter;
    private ViewPager viewPager;
    private String imgUrl = "https://rong.36kr.com/api/mobi/roundpics/v4";


    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            Message message = handler.obtainMessage();
            message.what = 1;
            handler.sendMessageDelayed(message, 5000);
            return false;
        }
    });
    private ArrayList<String> arrayList;


    @Override
    protected int setLayout() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initView() {
        viewPagerAdapter = new NewsViewPagerAdapter(MyApp.getContext());
        viewPager = bindView(R.id.fragment_discover_viewpager);
        LinearLayout recentLl = bindView(R.id.find_Recent_Event);
        recentLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FindRecentEventActivity.class);
                startActivity(intent);
            }
        });

        LinearLayout graduateLl = bindView(R.id.find_graduate_school);
        graduateLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FindRecentGraduateActivity.class);
                startActivity(intent);
            }
        });


    }

    @Override
    protected void initData() {
        arrayList = new ArrayList<>();


        mNetTool.getNetData(imgUrl, NewsCarouselBean.class, new NetTool.NetListener<NewsCarouselBean>() {
            @Override
            public void onSuccess(NewsCarouselBean newsCarouselBean) {
                for (int i = 0; i < newsCarouselBean.getData().getPics().size(); i++) {
                    arrayList.add(newsCarouselBean.getData().getPics().get(i).getImgUrl());
                }
                viewPagerAdapter.setmList(arrayList);
                viewPagerAdapter.setViewPager(viewPager);

                viewPager.setAdapter(viewPagerAdapter);
                viewPager.setCurrentItem(5000, false);


            }

            @Override
            public void onError(String errorMsg) {
                Toast.makeText(mContext, "无法获取图片", Toast.LENGTH_SHORT).show();
            }
        });
    }

    // 轮播图线程的生命周期
    @Override
    public void onStart() {
        super.onStart();
        Log.d("NewsFragment", "start");
        Message message = handler.obtainMessage();
        message.what = 1;
        handler.sendMessageDelayed(message, 5000);
    }

    @Override
    public void onStop() {
        super.onStop();
        handler.removeMessages(1);
    }
}
